﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using nova;
using PIMS.DBModel;
using PIMS.Interface;
using PIMS.WorkPlugin.ORM;
using PIMS.XmlModel;
using PIMS_Interface.Base;
using PIMS_Interface.Interface;
using SqlSugar;

namespace PIMS.WorkPlugin
{
    public partial class Form用户管理 : Form, IPlugin
    {
        public Form用户管理()
        {
            InitializeComponent();
            DgvTools.dataGridView(this.dgv);            
        }

        /// <summary>
        /// 注：反射加载时会自动调用一次
        /// </summary>
        public void Init(IDebugMsg debugMsg, ILog4net log4net, AllPLC allPLC, PIMSConfigX pimsConfig)
        {
        }

        /// <summary>
        /// 注：反射加载时会自动调用一次
        /// </summary>
        public void Init(IDebugMsg debugMsg, ILog4net log4net)
        {
        }

        private void Form用户管理_Load(object sender, EventArgs e)
        {
            this.dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            Refresh();
        }
        private void Refresh()
        {
            try
            {
                string cmd = "select * from tb_users where id > 1 order by id";
                //  DataTable dtUser = DbHelperSQL.Queryy(cmd, DbHelperSQL.connectionStringRBAC);
                DataTable dtUser = SqlSugarHelper.QueryToDataTable<users>(cmd);
                if (dtUser.Columns.Contains("Password")) dtUser.Columns.Remove("Password");
                this.dgv.DataSource = dtUser;

                DgvTools.dataGridViewAutoSize(this.dgv);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Form用户管理->Refresh");
            }

        }
        private void btn_Add_Click(object sender, EventArgs e)
        {
            Form增加编辑用户 fmchangpw = new Form增加编辑用户();
            fmchangpw.ShowDialog();
            fmchangpw.Close();
            Refresh();
        }
        private void btn_edit_Click(object sender, EventArgs e)
        {
            if(dgv.SelectedRows.Count!=1)
            {
                MessageBox.Show("请选中一行再进行操作！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int uid = Convert.ToInt16(dgv.SelectedRows[0].Cells[0].Value);
            Form增加编辑用户 fmchangpw = new Form增加编辑用户(uid);
            fmchangpw.ShowDialog();
            fmchangpw.Close();
            Refresh();
        }


        private void Form用户管理_Resize(object sender, EventArgs e)
        {
            this.dgv.Width = this.Size.Width;
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            if (dgv.SelectedRows.Count != 1)
            {
                MessageBox.Show("请选中一行再进行操作！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                users u = SqlSugarHelper.QueryByKey<users>(Convert.ToInt16(dgv.SelectedRows[0].Cells[0].Value));
                if (u != null)
                {
                    DialogResult dr = MessageBox.Show($"确认删除用户<{u.Description}>吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == DialogResult.Yes)
                    {
                        SqlSugarHelper.DeleteRecordByKey<users>(u);
                    }
                }


            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message,"Err");
            }
            Refresh();
        }
    }
}
